<%
Class FastAsp_Crypt
    Private m_Key    '加密密钥
    Private m_IV     '初始向量
    
    Private Sub Class_Initialize()
        m_Key = GetConfig("CryptKey")
        m_IV = GetConfig("CryptIV")
    End Sub
    
    '设置密钥
    Public Function SetKey(byval key)
        m_Key = key
        Set SetKey = Me
    End Function
    
    '设置向量
    Public Function SetIV(byval iv)
        m_IV = iv
        Set SetIV = Me
    End Function
    
    'MD5加密
    Public Function MD5(byval str)
        If IsNull(str) Then str = ""
        Dim obj : Set obj = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
        Dim bytes : bytes = obj.ComputeHash_2(StringToBytes(str))
        MD5 = BytesToHex(bytes)
        Set obj = Nothing
    End Function
    
    'SHA1加密
    Public Function SHA1(byval str)
        If IsNull(str) Then str = ""
        Dim obj : Set obj = CreateObject("System.Security.Cryptography.SHA1CryptoServiceProvider")
        Dim bytes : bytes = obj.ComputeHash_2(StringToBytes(str))
        SHA1 = BytesToHex(bytes)
        Set obj = Nothing
    End Function
    
    'AES加密
    Public Function AESEncrypt(byval str)
        If IsNull(str) Then str = ""
        Dim aes : Set aes = CreateObject("System.Security.Cryptography.RijndaelManaged")
        aes.Key = StringToBytes(m_Key)
        aes.IV = StringToBytes(m_IV)
        
        Dim bytes : bytes = StringToBytes(str)
        Dim encrypt : Set encrypt = aes.CreateEncryptor()
        bytes = encrypt.TransformFinalBlock(bytes, 0, LenB(str))
        
        AESEncrypt = BytesToBase64(bytes)
        Set encrypt = Nothing
        Set aes = Nothing
    End Function
    
    'AES解密
    Public Function AESDecrypt(byval str)
        If IsNull(str) Then str = ""
        Dim aes : Set aes = CreateObject("System.Security.Cryptography.RijndaelManaged")
        aes.Key = StringToBytes(m_Key)
        aes.IV = StringToBytes(m_IV)
        
        Dim bytes : bytes = Base64ToBytes(str)
        Dim decrypt : Set decrypt = aes.CreateDecryptor()
        bytes = decrypt.TransformFinalBlock(bytes, 0, LenB(str))
        
        AESDecrypt = BytesToString(bytes)
        Set decrypt = Nothing
        Set aes = Nothing
    End Function
    
    '生成随机密钥
    Public Function GenerateKey(byval length)
        Dim str, i
        Randomize
        For i = 1 To length
            str = str & Chr(Int(Rnd * 26) + 97)
        Next
        GenerateKey = str
    End Function
    
    '字符串转字节数组
    Private Function StringToBytes(byval str)
        Dim stream : Set stream = CreateObject("ADODB.Stream")
        stream.Type = 2 'adTypeText
        stream.Charset = "utf-8"
        stream.Open
        stream.WriteText str
        
        stream.Position = 0
        stream.Type = 1 'adTypeBinary
        StringToBytes = stream.Read
        stream.Close
        Set stream = Nothing
    End Function
    
    '字节数组转字符串
    Private Function BytesToString(byval bytes)
        Dim stream : Set stream = CreateObject("ADODB.Stream")
        stream.Type = 1 'adTypeBinary
        stream.Open
        stream.Write bytes
        
        stream.Position = 0
        stream.Type = 2 'adTypeText
        stream.Charset = "utf-8"
        BytesToString = stream.ReadText
        stream.Close
        Set stream = Nothing
    End Function
    
    '字节数组转16进制
    Private Function BytesToHex(byval bytes)
        Dim i, b, hex
        For i = 1 To LenB(bytes)
            b = AscB(MidB(bytes, i, 1))
            hex = hex & Right("0" & Hex(b), 2)
        Next
        BytesToHex = LCase(hex)
    End Function
    
    '字节数组转Base64
    Private Function BytesToBase64(byval bytes)
        Dim xml : Set xml = CreateObject("MSXML2.DOMDocument")
        Dim node : Set node = xml.createElement("b64")
        node.dataType = "bin.base64"
        node.nodeTypedValue = bytes
        BytesToBase64 = node.text
        Set node = Nothing
        Set xml = Nothing
    End Function
    
    'Base64转字节数组
    Private Function Base64ToBytes(byval base64)
        Dim xml : Set xml = CreateObject("MSXML2.DOMDocument")
        Dim node : Set node = xml.createElement("b64")
        node.dataType = "bin.base64"
        node.text = base64
        Base64ToBytes = node.nodeTypedValue
        Set node = Nothing
        Set xml = Nothing
    End Function
End Class
%> 